<template>
  <div class="login-container">
    <el-card
      v-loading="loading"
      element-loading-text="全力加速中"
      element-loading-spinner="el-icon-loading"
      element-loading-background="rgba(0, 0, 0, 0.2)"
    >
      <h3>用户登录</h3>
      <hr />
      <el-form :model="managerForm" :rules="loginRules" ref="loginref" label-width="80px">
        <el-form-item prop="mgName">
          <el-input v-model="managerForm.mgName" clearable placeholder="请输入用户名">
            <i slot="prefix" class="el-input__icon el-icon-user-solid"></i>
          </el-input>
        </el-form-item>
        <el-form-item prop="mgPwd">
          <el-input v-model="managerForm.mgPwd" show-password placeholder="请输入密码">
            <i slot="prefix" class="el-input__icon el-icon-success"></i>
          </el-input>
        </el-form-item>

        <el-form-item class="login-button">
          <el-button size="medium" type="success" @click="submitForm('loginref')">登录</el-button>
          <el-button size="medium" type="primary" @click="resetForm('loginref')">重置</el-button>
        </el-form-item>
      </el-form>
    </el-card>
  </div>
</template>

<script>
export default {
  name: "Login",
  data() {
    return {
      managerForm: {
        mgName: "",
        mgPwd: ""
      },
      loginRules: {
        mgName: [{ required: true, message: "请输入用户名", trigger: "blur" }],
        mgPwd: [
          {
            required: true,
            validator: this.validatePwd,
            trigger: "blur"
          }
        ]
      },
      loading: false
    };
  },
  methods: {
    submitForm(loginref) {
      this.$refs[loginref].validate(valid => {
        if (valid) {
          this.loading = true;
          let params = new URLSearchParams();
          params.append("mgName", this.managerForm.mgName);
          params.append("mgPwd", this.managerForm.mgPwd);
          this.$http
            .post("/manager/noAuthorize/auth", params)
            .then(resp => {
              this.loading = false;
              let { code, msg } = resp.data;
              let { token, realName, pIdList, managerId } = resp.data.data;
              if (code === 1) {
                // 放入sessionStore中
                sessionStorage.setItem("realName", realName);
                sessionStorage.setItem("managerId", managerId);
                sessionStorage.setItem("CR-TOKEN", token);
                sessionStorage.setItem("pIdList", JSON.stringify(pIdList));
                this.$message({
                  message: "欢迎使用！",
                  center: true,
                  type: "success"
                });
                return this.$router.push("/home");
              }
              return this.$message.error(msg);
            })
            .catch(resp => {
              this.loading = false;
            });
        } else {
          console.log("error submit!!");
          return false;
        }
      });
    },
    resetForm(loginref) {
      this.$refs[loginref].resetFields();
    },
    validatePwd(rule, value, callback) {
      const reg = /^(?![0-9]+$)(?![a-zA-Z]+$)[0-9A-Za-z]{5,16}$/;
      if (reg.test(value)) {
        return callback();
      }
      callback(new Error("5-16位，密码要包含数字和字母"));
    }
  }
};
</script>

<style scoped>
.login-container {
  height: 100%;
  background-color: #2b4b6b;
}
.el-card {
  width: 450px;
  height: 350px;
  left: 455px;
  top: 120px;
  position: absolute;
  border-radius: 10px;
}
.el-form {
  width: 330px;
  margin-top: 30px;
}

h3 {
  text-align: center;
}
.login-button {
  margin-left: 45px;
}
</style>